package P003;

import org.junit.Test;

/**
 * @Author DJ同学
 * @Date 2021/2/7 18:14
 * @Version 1.0
 * @Name 长度最小的子数组
 * @Problem https://leetcode-cn.com/problems/minimum-size-subarray-sum/
 */
public class Solution209 {

    @Test
    public void test01(){
        System.out.println(minSubArrayLen(7,new int[]{2,3,1,2,4,3}));

    }
    public int minSubArrayLen(int target, int[] nums) {
        int begin = 0;
        int end = 0;
        int minLen = 999999;
        int cur=0;
        int f = 0;
        while(end<nums.length){
            cur+=nums[end];
            if(cur>=target){
                //一直挪动begin
                while(cur>=target){
                    minLen =Math.min(minLen,end-begin+1);
                    cur -= nums[begin];
                    begin = begin + 1;
                }
            }

            end++;
        }
        return minLen == 999999?0:minLen;
    }
}
